<template>
  <el-dialog
    v-model="visible"
    :title="`${mode === 'add' ? '新增' : '编辑'}学员`"
    width="50%"
    top="5vh"
  >
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="学员姓名" prop="studentName">
            <el-input v-model="form.studentName" placeholder="请输入学员姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="form.gender">
              <el-radio 
                v-for="item in GENDER_OPTIONS" 
                :key="item.value" 
                :value="item.value"
              >
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="年龄" prop="age">
            <el-input-number v-model="form.age" :min="3" :max="99" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入联系方式" />
          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="学员来源" prop="source">
            <el-select v-model="form.source" placeholder="请选择学员来源">
              <el-option
                v-for="item in SOURCE_OPTIONS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学员状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择学员状态">
              <el-option
                v-for="item in STATUS_OPTIONS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="生日" prop="birthday">
        <el-radio-group v-model="form.birthdayType" style="margin-right: 16px">
          <el-radio-button value="1">阳历</el-radio-button>
          <el-radio-button value="0">农历</el-radio-button>
        </el-radio-group>
        <el-date-picker
          v-model="form.birthday"
          type="date"
          placeholder="选择生日"
          value-format="YYYY-MM-DDTHH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="入学时间" prop="enrollDate">
        <el-date-picker
          v-model="form.enrollDate"
          type="date"
          placeholder="选择入学日期"
          value-format="YYYY-MM-DDTHH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  GENDER_OPTIONS,
  SOURCE_OPTIONS,
  STATUS_OPTIONS
} from '@/constants/student'

const props = defineProps({
  modelValue: Boolean,
  studentData: Object,
  mode: String // 'add' or 'edit'
})

const emit = defineEmits(['update:modelValue', 'success'])

const visible = ref(false)
const formRef = ref(null)

// 表单数据
const form = reactive({
  studentName: '',
  gender: '1',
  age: 10,
  phone: '',
  source: '1',
  status: '3',
  birthday: '',
  birthdayType: '1',
  enrollDate: '',
  remark: '',
})

// 表单验证规则
const rules = {
  studentName: [{ required: true, message: '请输入学员姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  phone: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }],
  enrollDate: [{ required: true, message: '请选择入学日期', trigger: 'change' }],
  source: [{ required: true, message: '请选择学员来源', trigger: 'change' }],
  status: [{ required: true, message: '请选择学员状态', trigger: 'change' }],
  birthday: [{ required: false, message: '请选择生日', trigger: 'change' }],
}

// 监听显示状态
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val && props.mode === 'edit' && props.studentData) {
    Object.assign(form, props.studentData)
  } else if (val && props.mode === 'add') {
    resetForm()
  }
})

watch(visible, (val) => {
  emit('update:modelValue', val)
})

// 重置表单
const resetForm = () => {
  formRef.value?.resetFields()
  Object.assign(form, {
    studentName: '',
    gender: '1',
    age: 10,
    phone: '',
    source: '1',
    status: '3',
    birthday: '',
    birthdayType: '1',
    enrollDate: '',
    remark: ''
  })
}

// 提交表单
import { insertStudent, updateStudent } from '@/api/student'

const submitForm = async () => {
  try {
    await formRef.value.validate()
    
    const api = props.mode === 'add' ? insertStudent : updateStudent
    const res = await api(form)
    
    if (res.code === 200) {
      ElMessage.success(props.mode === 'add' ? '新增成功' : '修改成功')
      emit('success')
      visible.value = false
    } else {
      ElMessage.error(res.message || '操作失败')
    }
  } catch (error) {
    console.error('操作失败', error)
    ElMessage.error('操作失败，请重试')
  }
}
</script>
